﻿using System.Linq;
using System;

namespace TicketingCompany
{
    public class GetOperations
    {
        public static string GetLocationNameByID(int id)
        {
            TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
            var locationName = dbContext.Locations.FirstOrDefault(l => l.LocationID == id).Name;
            return locationName;
        }

        public static string GetCategoryNameByID(int id)
        {
            TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
            var categoryName = dbContext.Categories.Single(c => c.CategoryID == id).Name;
            return categoryName;
        }

        public static bool DeleteEventByID(int ID)
        {
            try
            {
                TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                using (dbContext)
                {
                    var eventToDelete = dbContext.Events.Single(e => e.EventID == ID);
                    dbContext.DeleteObject(eventToDelete);
                    dbContext.SaveChanges();
                    return true;
                }
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        public static bool IsSafeToDeleteLocationWithID(int id)
        {
            using (TicketingCompanyEntities dbContext = new TicketingCompanyEntities())
            {
                var events = dbContext.Locations.FirstOrDefault(l => l.LocationID == id).Events;
                if (events.Count == 0)
                {
                    return true;
                }
                return false;
            }
        }

        public static bool IsSafeToDeleteCategoryWithID(int id)
        {
            using (TicketingCompanyEntities dbContext = new TicketingCompanyEntities())
            {
                var events = dbContext.Categories.FirstOrDefault(c => c.CategoryID == id).Events;
                if (events.Count == 0)
                {
                    return true;
                }
                return false;
            }
        }

        public static Event GetEventByID(int id)
        {
            using (TicketingCompanyEntities dbContext = new TicketingCompanyEntities())
            {
                var eve = dbContext.Events.FirstOrDefault(e => e.EventID == id);
                return eve;
            }
        }

        public static IQueryable<Event> GetIncomingEvents()
        {
            TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
            var now = DateTime.Now.AddDays(3);
            var result = dbContext.Events.Where(e => e.DateTime > now);
            return result;

        }

        public static void CreatePurchase(Purchase toAdd)
        {
            TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
            using (dbContext)
            {
                dbContext.Purchases.AddObject(toAdd);
                dbContext.SaveChanges();
            }
        }

        public bool ChangePurchaseDeliverStatus(int id)
        {
            TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
            using (dbContext)
            {
                try
                {
                    var pur = dbContext.Purchases.FirstOrDefault(p => p.PurchaseID == id);
                    pur.Delivered = !pur.Delivered;
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
                
            }
        }
    }
}